package com.rjkj.medicalwaste.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * Created by Administrator on 2018/12/20.
 */

public class DeviceIdentUilts {

    @SuppressLint("HardwareIds")
    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceIdStr;
        if (tm.getDeviceId() != null) {
            deviceIdStr = tm.getDeviceId();
        } else {
            deviceIdStr = "";
        }

//        if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {//8.0及以后版本获取
//            deviceIdStr = tm.getDeviceId();
//        }else{//9.0到10.0获取
//            deviceIdStr = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//        }
        Log.e("IMEI", deviceIdStr);//IMEI第一段
        return deviceIdStr;
    }

    @SuppressLint("HardwareIds")
    public static String getIMEIDeviceId(Context context) {
        String deviceId;
        final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return "";
            }
        }
        assert mTelephony != null;
        if (mTelephony.getDeviceId() != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                deviceId = mTelephony.getImei();
                Log.e("IMEI", "getImei: " + deviceId);
            } else {
                deviceId = mTelephony.getDeviceId();
                Log.e("IMEI", "getDeviceId: " + deviceId);
            }
        } else {
//            deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//            Log.e("IMEI", "getAndroidId: " + deviceId);
            deviceId = "获取失败，请输入";
        }

        return deviceId;
//        try {
//            if (mTelephony.getDeviceId() != null)
//            {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
//                {
//                    deviceId = mTelephony.getImei();
//                }else {
//                    deviceId = mTelephony.getDeviceId();
//                }
//            } else {
//                deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//            }
//            Log.d("deviceId", deviceId);
//            return deviceId;
//        }catch (SecurityException e){
//            e.printStackTrace();
//        }
//        String s = UUID.randomUUID().toString();
//        Log.e("UUID", "getIMEIDeviceId: "+s );
//        return s;

    }

    public static String getMEID(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Method method = null;
        String meid = "";
        try {
            method = tm.getClass().getMethod("getDeviceId", int.class);
            meid = (String) method.invoke(tm, 2);
            Log.e("meid", meid);//MEID
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return meid;
    }

    public static String getSerial() {
        String serialnum = "";
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");

            Method get = c.getMethod("get", String.class, String.class);

            serialnum = (String) (get.invoke(c, "ro.serialno", "unknown"));

        } catch (Exception ignored) {
        }
        return serialnum;
    }

    /*测试*/
    public static void test(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Method method = null;
        try {
            method = manager.getClass().getMethod("getDeviceId", int.class);
            String imei1 = manager.getDeviceId();

            String imei2 = (String) method.invoke(manager, 1);
            Log.e("imei2", imei2);//IMEI第二段
            String meid = (String) method.invoke(manager, 2);
            Log.e("meid", meid);//MEID
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
